package com.ruoyi.common.utils.mail;

import com.ruoyi.common.utils.mail.domain.MailContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.TimerTask;

/**
 * @Author: MMDZ
 * @Desc: TODO 发送邮件
 */
@Component
public class SendMailUtil {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JavaMailSender sender;

    @Value("${spring.mail.username}")
    private String formMail;

    /**
     * 发送纯文本的简单邮件
     * @param mailContent
     */
    public TimerTask sendSimpleMail(MailContent mailContent){
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SimpleMailMessage message = new SimpleMailMessage();
                message.setFrom(formMail);
                message.setTo(mailContent.getToMail().split(";"));
                message.setSubject(mailContent.getSubject());
                message.setText(mailContent.getContent());
                logger.info("测试邮件");
                try {
                    sender.send(message);
                    logger.info("发送成功");
                } catch (Exception e) {
                    logger.error("发送邮件发生异常！", e);
                }
            }
        };
    }

}
